using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Drawing.Drawing2D;
using Autorun;
using System.Diagnostics;

namespace Autorun.Client.Controls
{
	public partial class ProcessorMonitor : PaneCaption
	{
		#region "Private members"
		ServerManagement _proxy;
		private LinearGradientBrush brBack;
		private Pen pUsage = new Pen(Color.DarkOrchid);
		private Pen pLines = new Pen(Color.BlueViolet);
		#endregion

		#region "Constructors and Form's Events Handlers"
		public ProcessorMonitor()
		{
			InitializeComponent();
			brBack = new LinearGradientBrush(DisplayRectangle, Color.Indigo, Color.MediumVioletRed, LinearGradientMode.Horizontal);
		}
		#endregion

		#region "Methods"
		public void SetData(ServerManagement proxy)
		{
			_proxy = proxy;
		}
		#endregion

		#region "Controls Events Handlers"
		private void pbChart_Paint(object sender, PaintEventArgs e)
		{
			Bitmap b;
			Graphics g;
			int[,] data = new int[4, 480];

			// === In any case, draw the background ================================
			b = new Bitmap(pbChart.Width, pbChart.Height, e.Graphics);
			g = Graphics.FromImage(b);

			g.FillRectangle(brBack, 0, 0, 480, 100);
			g.DrawLine(pLines, 0, 25, 480, 25);
			g.DrawLine(pLines, 0, 50, 480, 50);
			g.DrawLine(pLines, 0, 75, 480, 75);
			g.DrawLine(pLines, 60, 0, 60, 100);
			g.DrawLine(pLines, 120, 0, 120, 100);
			g.DrawLine(pLines, 180, 0, 180, 100);
			g.DrawLine(pLines, 240, 0, 240, 100);
			g.DrawLine(pLines, 300, 0, 300, 100);
			g.DrawLine(pLines, 360, 0, 360, 100);
			g.DrawLine(pLines, 420, 0, 420, 100);

			if (_proxy != null)
				lock (this) //Added to prevent proxy from being released when loosing connection
				{
					data = _proxy.Monitor.Data;
					for (int col = 0; col < 480; col++)
						g.DrawLine(pUsage, 480 - col, 100, 480 - col, 100 - data[0, col]);
				}

			e.Graphics.DrawImageUnscaled(b, 0, 0);
		}

		private void myTimer_Tick(object sender, EventArgs e)
		{
			int resX;

			lblTime.Text = DateTime.Now.ToString("HH:mm:ss");
			if (_proxy != null)
				resX = _proxy.Monitor.Resolution;
			else
				resX = 1;
			lblT75.Text = DateTime.Now.AddSeconds(-120 * resX).ToString("HH:mm:ss");
			lblT50.Text = DateTime.Now.AddSeconds(-240 * resX).ToString("HH:mm:ss");
			lblT25.Text = DateTime.Now.AddSeconds(-360 * resX).ToString("HH:mm:ss");
			lblT00.Text = DateTime.Now.AddSeconds(-480 * resX).ToString("HH:mm:ss");
			pbChart.Invalidate();
		}
		#endregion
	}
}
